#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;
int n, m;
int a[N], b[N];
int pref[N], suf[N]; // 新增两个数组

void solve() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) cin >> b[i];

    // 1. 计算前缀匹配位置
    int p = 1;
    pref[0] = 0;
    for (int i = 1; i <= m; i++) pref[i] = INF; // 初始化
    for (int i = 1; i <= n && p <= m; i++) {
        if (a[i] >= b[p]) {
            pref[p] = i;
            p++;
        }
    }
    if (pref[m] != INF) { // 全部匹配成功，不用插
        cout << 0 << '\n';
        return;
    }

    // 2. 计算后缀匹配位置
    int q = m;
    suf[m + 1] = n + 1;
    for (int i = m; i >= 1; i--) suf[i] = 0; // 初始化
    for (int i = n; i >= 1 && q >= 1; i--) {
        if (a[i] >= b[q]) {
            suf[q] = i;
            q--;
        }
    }

    // 3. 枚举插入替代 b[t]
    int ans = INF;
    for (int t = 1; t <= m; t++) {
        // 前缀 b[1..t-1] 要能匹配，后缀 b[t+1..m] 要能匹配
        if (pref[t - 1] != INF && suf[t + 1] != 0 && pref[t - 1] < suf[t + 1]) {
            ans = min(ans, b[t]);
        }
}

    if (ans == INF) cout << -1 << '\n';
    else cout << ans << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int T;
    cin >> T;
    while (T--) solve();
}
